//
//  LMImageEditorViewController.m
//  Perking
//
//  Created by longmin on 16/7/11.
//  Copyright © 2016年 tim. All rights reserved.
//

#import "LMImageEditorViewController.h"
#import "UIImage+SnapShot.h"
#import "LMButton.h"

@interface LMImageEditorViewController ()<UIScrollViewDelegate>

@property(nonatomic, strong) UIScrollView *scrollView;
/** 调整的图片 */
@property(nonatomic, strong) UIImageView *imageView;
/** 裁剪框 */
@property(nonatomic, strong) UIView *cutView;
/** 上面的遮挡图 */
@property(nonatomic, strong) UIView *topBGView;
/** 下面的遮挡图 */
@property(nonatomic, strong) UIView *bottomBGView;
/** 左边的遮挡图 */
@property(nonatomic, strong) UIView *leftBGView;
/** 右边的遮挡图 */
@property(nonatomic, strong) UIView *rightBGView;

@end

@implementation LMImageEditorViewController

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    //scrollView
    [self createScrollView];
    
    //裁剪框
    [self.view addSubview:self.cutView];
    [self.view addSubview:self.topBGView];
    [self.view addSubview:self.bottomBGView];
    [self.view addSubview:self.leftBGView];
    [self.view addSubview:self.rightBGView];
    //底部视图
    [self createBottomView];

}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.cutView.frame = self.clipRect;
    self.topBGView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.cutView.frame.origin.y);
    self.bottomBGView.frame = CGRectMake(0,
                                         CGRectGetMaxY(self.cutView.frame),
                                         self.view.bounds.size.width,
                                         self.view.bounds.size.height - CGRectGetMaxY(self.cutView.frame));
    
    self.leftBGView.frame = CGRectMake(0,
                                       self.cutView.frame.origin.y,
                                       self.cutView.frame.origin.x,
                                       self.cutView.frame.size.height);
    
    self.rightBGView.frame = CGRectMake(CGRectGetMaxX(self.cutView.frame),
                                        self.cutView.frame.origin.y,
                                        self.view.bounds.size.width - CGRectGetMaxX(self.cutView.frame),
                                        self.cutView.frame.size.height);
    
}

#pragma mark - set方法和get方法

-(UIView *)topBGView {
    if (_topBGView == nil) {
        _topBGView = [[UIView alloc] init];
        self.topBGView.backgroundColor = kRGBACOLOR(0, 0, 0, 0.3);
        self.topBGView.userInteractionEnabled = NO;
    }
    return _topBGView;
}

-(UIView *)bottomBGView {
    if (_bottomBGView == nil) {
        _bottomBGView = [[UIView alloc] init];
        self.bottomBGView.backgroundColor = self.topBGView.backgroundColor;
        self.bottomBGView.userInteractionEnabled = NO;
    }
    return _bottomBGView;
}

-(UIView *)leftBGView {
    if (_leftBGView == nil) {
        _leftBGView = [[UIView alloc] init];
        self.leftBGView.backgroundColor = self.topBGView.backgroundColor;
        self.leftBGView.userInteractionEnabled = NO;
    }
    return _leftBGView;
}

-(UIView *)rightBGView {
    if (_rightBGView == nil) {
        _rightBGView = [[UIView alloc] init];
        self.rightBGView.backgroundColor = self.topBGView.backgroundColor;
        self.rightBGView.userInteractionEnabled = NO;
    }
    return _rightBGView;
}

-(UIView *)cutView{
    if (_cutView == nil) {
        _cutView = [[UIView alloc] init];
        _cutView.layer.borderColor = self.clipColor ? self.clipColor.CGColor : [UIColor whiteColor].CGColor;
        _cutView.layer.borderWidth = 1.0;
        _cutView.userInteractionEnabled = NO;
    }
    return _cutView;
}
-(UIImageView *)imageView{
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode=UIViewContentModeScaleAspectFit;
    }
    return _imageView;
}
-(UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        self.scrollView.delegate = self;
        self.scrollView.maximumZoomScale = 3.0;
        self.scrollView.minimumZoomScale = 1.0;
        self.scrollView.bounces = YES;
        self.scrollView.alwaysBounceVertical = YES;
        self.scrollView.alwaysBounceHorizontal = YES;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.backgroundColor = [UIColor blackColor];
    }
    return _scrollView;
}
#pragma mark - IBActions
#pragma mark - Public
#pragma mark - Private
-(void)createScrollView{
    
    [self.view addSubview:self.scrollView];
    //被裁剪的图片
//    self.imageView.frame = self.view.bounds;
    self.imageView.image = self.originalImage;
    [self.imageView sizeToFit];
    
    
    [self.scrollView addSubview:self.imageView];
    
    
    self.scrollView.frame = self.view.bounds;
    
    if (self.imageView.width > self.scrollView.width) {
        CGFloat scaleW = self.imageView.width / self.scrollView.width;
        self.imageView.width = self.scrollView.width;
        self.imageView.height /= scaleW;
    }
    
    
    self.scrollView.contentSize = CGSizeMake(self.imageView.width, self.imageView.height);
    self.imageView.center = CGPointMake(self.scrollView.contentSize.width*0.5, self.scrollView.contentSize.height*0.5);

    
    CGFloat topInset = self.clipRect.origin.y;
    CGFloat leftInset = self.clipRect.origin.x;
    CGFloat bottomInset = self.scrollView.height - self.clipRect.origin.y - self.clipRect.size.height;
    CGFloat rightInset = self.scrollView.width - self.clipRect.origin.x - self.clipRect.size.width;
    
    self.scrollView.contentInset = UIEdgeInsetsMake(topInset,
                                                    leftInset,
                                                    bottomInset,
                                                    rightInset);

    
    
    CGFloat offsetCX = (leftInset + rightInset + self.scrollView.contentSize.width) * 0.5;
    CGFloat offsetCY = (topInset + bottomInset + self.scrollView.contentSize.height) * 0.5;
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    self.scrollView.contentOffset = CGPointMake(offsetCX-screenW*0.5, offsetCY-screenH * 0.5-bottomInset);
    
}

-(void)createBottomView{
    //底部视图
    UIView *bottomView = [[UIView alloc] init];
    bottomView.width = self.view.width;
    bottomView.height = 64;
    bottomView.left = 0;
    bottomView.top = self.view.height - bottomView.height;
    bottomView.backgroundColor = kRGBACOLOR(30, 30, 30, 0.5);
    [self.view addSubview:bottomView];
    //取消按钮
    CGFloat btnW = 40;
    LMButton *cancelBtn = [[LMButton alloc] init];
    cancelBtn.frame = CGRectMake(20, 0, btnW, bottomView.height);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    
    __weak typeof(self) weakSelf = self;
    cancelBtn.onClick = ^(UIButton *btn){
        NSLog(@"取消图片编辑");
        [weakSelf.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{
            
        }];

        /*
        [weakSelf dismissViewControllerAnimated:YES completion:^{
            if (weakSelf.completeBlock) {
                weakSelf.completeBlock(nil);
            }
        }];
         */
    };
    [bottomView addSubview:cancelBtn];
    //确定按钮
    LMButton *chooseBtn = [[LMButton alloc] init];
    chooseBtn.frame = CGRectMake(bottomView.width - btnW - 20, 0, btnW, bottomView.height);
    [chooseBtn setTitle:@"选取" forState:UIControlStateNormal];
    [bottomView addSubview:chooseBtn];
    
    chooseBtn.onClick = ^(UIButton *btn){
        NSLog(@"确定选择图片");
        weakSelf.cutView.layer.borderColor = [UIColor clearColor].CGColor;
        UIImage *resultImage = [UIImage screenShotWithView:weakSelf.view forRect:weakSelf.clipRect];
        //把处理好的图片通过block返回
        if (weakSelf.completeBlock) {
            weakSelf.completeBlock(resultImage);
        }
        [weakSelf.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{
            
        }];

    };
}


#pragma mark - 数据源和代理方法

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.imageView;
}
@end
